"use strict";
var prefix = "/report/naturalPerson";
var naturalPersonId = $("#naturalPersonId").val();
var QRCode,batch;
$(function(){
	/*$("body").on("click",".basic-type-img",function(){
		$(this).parent().siblings(".detail-type").slideToggle();
		if($(this).hasClass("basic-type-img-bottom")){
			$(this).removeClass("basic-type-img-bottom");
			$(this).addClass("basic-type-img-top");
		}else if($(this).hasClass("basic-type-img-top")){
			$(this).removeClass("basic-type-img-top");
			$(this).addClass("basic-type-img-bottom");
		}
	})

	$("body").on("click",".detail-type-img",function(){
		$(this).parent().siblings("table").fadeToggle();
		if($(this).hasClass("detail-type-img-sub")){
			$(this).removeClass("detail-type-img-sub");
			$(this).addClass("detail-type-img-add");
		}else if($(this).hasClass("detail-type-img-add")){
			$(this).removeClass("detail-type-img-add");
			$(this).addClass("detail-type-img-sub");
		}
	})*/
	init(naturalPersonId);
	fun();
	QRCode();
})

/**
 * 查询当前信用记录报告
 * @returns
 */
function init(naturalPersonId){
	$.ajax({
		data : {
			naturalPersonId : naturalPersonId
		}, 
		url : prefix + '/detail' ,
		async : false,
		success : function(r) {
			if (r.code != 0) {
				layer.msg(r.msg);
			}else{
				var Str = "";
				var data = r.obj;
				$.each(data,function (key1,value1){
					Str += `<div class="basic-type">
								<div class="basic-type-head clearfix">
									<h2>${key1}</h2>
									<div class="basic-type-img basic-type-img-bottom"></div>
								</div>`;
					$.each(value1,function (key2,value2){
						if(key2 == "身份信息"){	// 遍历身份信息
						Str += `<div class="detail-type">
									<div class="detail-type-head clearfix">
										<h3>${key2}</h3>
										<div class="detail-type-img detail-type-img-add"></div>
									</div>
									<table class="registration-message">`;
							for(let i = 0;i<value2.length;i = i+2){
								if(i == (value2.length-1) ){
								Str +=	`<tr>
											<td>${value2[i].busi_name}</td>
											<td colspan="3">${value2[i].busi_value}</td>
										</tr>`	
								}else{
								Str +=	`<tr>
											<td>${value2[i].busi_name}</td>
											<td>${value2[i].busi_value}</td>
											<td>${value2[i+1].busi_name}</td>
											<td>${value2[i+1].busi_value}</td>
										</tr>`
								}
							}
						Str +=		`</table>
								</div>`;
						}else{	// 遍历其他信息
						Str += `<div class="detail-type">
									<div class="detail-type-head clearfix">
										<h3>${key2}</h3>
										<div class="detail-type-img detail-type-img-add"></div>
									</div>
									<table class="other-message">`;
							if(value2.length == 0){	// 判断是否存在记录
								Str +=	`<tr>
											<td colspan="2">无记录</td>
											</tr>`
							}else{
								$.each(value2,function(key3,value3){
									switch(value3.link_way)	// 根据链接类型确定对应的返回
									{
									case 0:
										Str +=	`<tr>
													<td>${value3.busi_name}</td>
													<td><a href="javaScript:void(0)" onclick="queryDetail('${naturalPersonId}','${value3.busi_id}',this)">${value3.busi_value}</></td>
												</tr>`
									  break;
									case 1:
										Str +=	`<tr>
													<td>${value3.busi_name}</td>
													<td><a href="javaScript:void(0)" onclick="queryLegal('${naturalPersonId}','${value3.busi_id}',this)">${value3.busi_value}</></td>
												</tr>`
									  break;
									case 2:
										Str +=	`<tr>
													<td>${value3.busi_name}</td>
													<td><a href="javaScript:void(0)" onclick="queryNatural('${naturalPersonId}','${value3.busi_id}',this)">${value3.busi_value}</></td>
												</tr>`
										  break;
									case 3:
										Str +=	`<tr>
													<td>${value3.busi_name}</td>
													<td>${value3.busi_value}</td>
												</tr>`
										  break;
									default:
										
									}
								})
							}
						Str +=		`</table>
								</div>`;
						}
					})			
								
					Str += `</div>`;
				})
				$("#msg").html(Str);
			}
		}
	})
}

/**
 * 查询明细
 * @param naturalPersonId	自然人ID
 * @param busiId	指标ID
 * @param _this	当前元素节点
 * @returns
 */
function queryDetail(naturalPersonId,busiId,_this){
	if($(_this).parents("tr").next().find(".detail-message").length != 0){
		//$(_this).parents("tr").next().fadeToggle();
	}else{
		$.ajax({
			data : {
				naturalPersonId : naturalPersonId,
				busiId : busiId
			}, 
			url : prefix + '/linkReport' ,
			success : function(r) {
				if(r.th.length <=0){
					window.open("/report/legalPerson/viewDetail/" + r.tr[0].legal_person_id)
				}else{
					var Str = `<tr>
									<td colspan="2">
										<div>
											<table class="detail-message">
												<tbody>
													<tr>`
							$.each(r.th,function(key1,value1){
								Str += `				<th>${value1}</th>`
							})
								Str +=				`</tr>`
							$.each(r.tr,function(key2,value2){
								Str +=				`<tr>`
								$.each(value2,function(key3,value3){
										Str +=			`<td>${value3}</td>`
								})
								Str +=				`</tr>`
							})
							Str +=				`</tbody>
											</table>
										</div>
									</td>
								</tr>`;
					$(_this).parents("tr").after(Str)
				}
			}
		})
	}
}

/**
 * 查询法人信用报告
 * @param naturalPersonId	自然人ID
 * @param busiId	指标ID
 * @param _this	当前元素节点
 * @returns
 */
function queryLegal(legalPersonId,busiId,_this){
	if($(_this).parents("tr").next().find(".detail-message").length != 0){
		//$(_this).parents("tr").next().fadeToggle();
	}else{
		$.ajax({
			data : {
				legalPersonId : legalPersonId,
				busiId : busiId
			}, 
			async: false,
			url : '/report/legalPerson/linkReport' ,
			success : function(r) {
				if(r.th.length <=0){
					window.open("/report/legalPerson/viewDetail/" + r.tr[0].legal_person_id)
				}else{
					var Str = `<tr>
									<td colspan="2">
										<div>
											<table class="detail-message">
												<tbody>
													<tr>`
							$.each(r.th,function(key1,value1){
								Str += `				<th>${value1}</th>`
							})
								Str +=				`</tr>`
							$.each(r.tr,function(key2,value2){
								Str +=				`<tr class="cursor" onclick="javaScript:window.open('/report/legalPerson/viewDetail/${value2.legal_person_id}')">`
								$.each(value2,function(key3,value3){
									if(key3 != "legal_person_id"){
										Str +=			`<td>${value3}</td>`
									}
								})
								Str +=				`</tr>`
							})
							Str +=				`</tbody>
											</table>
										</div>
									</td>
								</tr>`;
					$(_this).parents("tr").after(Str)
				}
			}
		})
	}
}

/**
 * 查询自然人信用报告
 * @param naturalPersonId	自然人ID
 * @param busiId	指标ID
 * @param _this	当前元素节点
 * @returns
 */
function queryNatural(naturalPersonId,busiId,_this){
	if($(_this).parents("tr").next().find(".detail-message").length != 0){
		//$(_this).parents("tr").next().fadeToggle();
	}else{
		$.ajax({
			data : {
				naturalPersonId : naturalPersonId,
				busiId : busiId
			}, 
			url : '/report/naturalPerson/linkReport' ,
			success : function(r) {
				if(r.th.length <=0){
					window.open("/report/naturalPerson/viewDetail/" + r.tr[0].natural_person_id)
				}else{
					var Str = `<tr>
									<td colspan="2">
										<div>
											<table class="detail-message">
												<tbody>
													<tr>`
							$.each(r.th,function(key1,value1){
								Str += `				<th>${value1}</th>`
							})
								Str +=				`</tr>`
							$.each(r.tr,function(key2,value2){
								Str +=				`<tr class="cursor" onclick="javaScript:window.open('/report/legalPerson/viewDetail/${value2.natural_person_id}')">`
								$.each(value2,function(key3,value3){
									if(key3 != "natural_person_id"){
										Str +=			`<td>${value3}</td>`
									}
								})
								Str +=				`</tr>`
							})
							Str +=				`</tbody>
											</table>
										</div>
									</td>
								</tr>`;
					$(_this).parents("tr").after(Str)
				}
			}
		})
	}
}


function fun(){
	for(var i = 0;i<$("a").length;i++){
		if($($("a")[i]).attr("onclick") != undefined){
			$($("a")[i]).click();
		}
	}
	$("tr:hidden").fadeIn();
	$("table:hidden").fadeIn();
	$("div.detail-type:hidden").fadeIn();
}

/**
 * 二维码
 * @returns
 */
function QRCode(){
	$.ajax({
		url: prefix + "/getQRCodeMessage",
		success : function(r) {
			 var str = JSON.stringify(r)
			 $('#QRCode').qrcode({
		          render:"canvas",
		          height:120,
		          width:120,
		          correctLevel:0,
		          text:toUtf8(str)
		      }); 
			 QRCode = toUtf8(str);
			 $("#pc").append(r.timeStamp);
			 batch = r.timeStamp;
		}
	})
}

  function toUtf8(str) {
      var out, i, len, c;
      out = "";
      len = str.length;
      for (i = 0; i < len; i++) {
          c = str.charCodeAt(i);
          if ((c >= 0x0001) && (c <= 0x007F)) {
              out += str.charAt(i);
          } else if (c > 0x07FF) {
              out += String.fromCharCode(0xE0 | ((c >> 12) & 0x0F));
              out += String.fromCharCode(0x80 | ((c >> 6) & 0x3F));
              out += String.fromCharCode(0x80 | ((c >> 0) & 0x3F));
          } else {
              out += String.fromCharCode(0xC0 | ((c >> 6) & 0x1F));
              out += String.fromCharCode(0x80 | ((c >> 0) & 0x3F));
          }
      }
      return out;
  }
  
  function printReport(){
	  window.print();
	  $.ajax({
		  url : prefix + "/snapShot",
		  type : "POST",
		  data:{
			  naturalPersonId: naturalPersonId,
			  QRCode: QRCode,
			  batch: batch,
			  type: 1,
			  content: $(".main").html()
		  },
		  success : function(r){
			  
		  }
	  })
  }

